package com.doit.demo02;

import javax.lang.model.element.VariableElement;
import java.io.*;
@SuppressWarnings("all")
public class copy01 {
    public static void main(String[] args) throws IOException {
        File srcDir = new File("D:\\work\\abc");
        File destDir = new File("D:\\wokk");

        long start = System.currentTimeMillis();
        copyF(srcDir,destDir);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    private static void copyF(File srcDir, File destDir) throws IOException {
        String name = srcDir.getName(); //获取目标名称

        File dest = new File(destDir, name);
        dest.mkdirs();//创建复制目录

        File[] srcFile = srcDir.listFiles();//获取文件

        for (File file : srcFile) {
            if (file.isFile()){
                File destfile = new File(dest, file.getName()); //创建文件
                copy(file,destfile);//复制到目标文件
            }
            else
                copyF(file,dest ); //文件夹的话也复制
        }
    }
    private static void copy(File src , File dest) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        int len;
        byte[] bytes = new byte[1024];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        out.close();
        in.close();
    }
}
